package com.gingko.duominuo.ui.fragment
import android.content.pm.PackageManager
import androidx.fragment.app.Fragment
import androidx.lifecycle.MutableLiveData
import com.gingko.duominuo.utils.LDPermission

internal class PermissionFragment : Fragment() {
    val refusedPermissionLiveData: MutableLiveData<List<String>> by lazy {
        MutableLiveData<List<String>>()
    }


    companion object {
        fun newInstance() = PermissionFragment()
    }

    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        if (requestCode == LDPermission.REQUEST_CODE) {
            val refusedPermissions = mutableListOf<String>()
            permissions.forEachIndexed { index, permission ->
                if (grantResults[index] != PackageManager.PERMISSION_GRANTED) {
                    refusedPermissions.add(permission)
                }
            }
            refusedPermissionLiveData.value = refusedPermissions
        }
    }
}